using Holoville.HOTween;
using UnityEngine;

public class HeliPropeller : MonoBehaviour
{
	public enum Type
	{
		MainRotor = 0,
		TailRotor = 1
	}

	public Type RotorType;

	public float ManualSpeed;

	public GameObject HeliBlades;

	public GameObject HitParticles;

	public GameObject BladesBlur;

	private float _hitForce;

	private float _heliEngineForceValue;

	private bool _hit;

	private float _startstopTime = 2f;

	private GameObject _heli;

	private HeliController heliController;

	private void Awake()
	{
		_heli = base.transform.parent.gameObject;
		heliController = _heli.GetComponent<HeliController>();
		ManualSpeed = 0f;
	}

	private void OnTriggerEnter(Collider col)
	{
		if (!col.isTrigger && _heliEngineForceValue > 0.3f)
		{
			_heli.GetComponent<HeliController>().HeliBladesHitting = true;
		}
	}

	private void OnTriggerStay(Collider col)
	{
		if (col.isTrigger)
		{
			return;
		}
		if (_heliEngineForceValue > 0.3f)
		{
			_hit = true;
			if (HitParticles != null)
			{
				HitParticles.GetComponent<ParticleSystem>().Play();
			}
			if (!col.GetComponent<Rigidbody>())
			{
				_hitForce = 10f;
			}
			else if (col.GetComponent<Rigidbody>().mass < 10f)
			{
				_hitForce = col.GetComponent<Rigidbody>().mass;
			}
			if (col.GetComponent<Rigidbody>() != null)
			{
				col.GetComponent<Rigidbody>().AddForce(-base.transform.right * _heliEngineForceValue * 100f);
				col.GetComponent<Rigidbody>().AddForce(Vector3.up * _heliEngineForceValue * 100f);
			}
		}
		else if (_heliEngineForceValue < 0.5f && HitParticles != null)
		{
			HitParticles.GetComponent<ParticleSystem>().Stop();
		}
	}

	private void OnTriggerExit(Collider col)
	{
		if (!col.isTrigger)
		{
			_heli.GetComponent<HeliController>().HeliBladesHitting = false;
			_hit = false;
		}
	}

	private void OnCollisionStay(Collision col)
	{
		HitParticles.transform.position = col.contacts[0].point;
	}

	private void FixedUpdate()
	{
		if (ManualSpeed != 0f)
		{
			_heliEngineForceValue = heliController.GetEngineForceValue();
			if (_hitForce > 0f)
			{
				heliController.RotateAround(_hitForce);
				_hitForce -= Time.deltaTime * 6f;
			}
			if (_hit)
			{
				heliController.EngineSlowDown(0.6f);
			}
			if (RotorType == Type.MainRotor)
			{
				float yAngle = (_heliEngineForceValue * 0.2f + ManualSpeed * 0.8f) * 600f * Time.deltaTime;
				HeliBlades.transform.Rotate(0f, yAngle, 0f);
			}
			else
			{
				HeliBlades.transform.Rotate((_heliEngineForceValue * 0.8f + ManualSpeed * 0.7f) * 600f * Time.deltaTime, 0f, 0f);
			}
			if (BladesBlur != null)
			{
				BladesBlur.GetComponent<Renderer>().material.color = new Color(1f, 1f, 1f, _heliEngineForceValue - 0.2f);
			}
		}
	}

	public void StartRotors()
	{
		HOTween.Kill(this);
		if (BladesBlur != null)
		{
			BladesBlur.SetActive(true);
		}
		HOTween.To(this, _startstopTime, new TweenParms().Prop("ManualSpeed", 1).Ease(EaseType.Linear));
	}

	public void StopRotors()
	{
		if (BladesBlur != null)
		{
			BladesBlur.SetActive(false);
		}
		HOTween.Kill(this);
		HOTween.To(this, _startstopTime * 5f, new TweenParms().Prop("ManualSpeed", 0).Ease(EaseType.Linear));
	}
}
